package com.guslegend.spzx.manager.demo;

import com.guslegend.spzx.model.entity.system.SysUser;

/**
 * @author: guslegend
 * @description: 线程工具类
 * @create: 2025-05-06 08:36
 **/
public class ThreadLocalTest {

    //创建一个ThreadLocal对象
    private static final ThreadLocal<SysUser> threadLocal=new ThreadLocal<>();

    public static void main(String[] args) {

        //在主线程中创建SysUser对象
        SysUser sysUser=new SysUser();
        sysUser.setUserName("admin");

        sysUser.setAvatar("https://oss.aliyuncs.com/aliyun_id_photo_bucket/default_handsome.jpg");
        sysUser.setId(1L);

        //将数据存储到ThreadLocal
        threadLocal.set(sysUser);

        //调用show方法,由于show方法main方法使用的是同一个线程,因此show方法可以直接从ThreadLocal中获取数据
        show();
    }

    private static void show() {
        SysUser sysUser=threadLocal.get();
        System.out.println(sysUser);
    }
}